from flask import Flask,render_template,request,flash
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField,SubmitField
from wtforms.validators import DataRequired,EqualTo    #验证函数

app = Flask(__name__)
app.secret_key = 'qweqwe'
class Login(FlaskForm):
    username = StringField('用户名',validators = [DataRequired()])
    PassWord = PasswordField('密码',validators = [DataRequired(),EqualTo('PassWord2','两次输入不一致')])
    #校验并设置报错语句，捕获错误可以拿到

    PassWord2 = PasswordField('确认密码',validators = [DataRequired()])
    submit = SubmitField('提交')

#使用wtf自定义表单,表单定义在python中完成
@app.route('/',methods=['GET','POST'])
def login():
    Form = Login()
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('PassWord')
        password2 = request.form.get('PassWord2')
        if Form.validate_on_submit():  #查看校验是否成功
            print(username,password)
            return 'success'
        else:
            return 'fail'

    return render_template('wtf_form.html',Form=Form)

if __name__ =='__main__':
    app.run()